package zmee.pur.src.formplugin.filter;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.scm.pds.common.extfilter.ExtFilterContext;
import kd.scm.pds.common.extfilter.IExtFilterPlugin;
import kd.scm.pds.common.util.PdsCommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: xw
 * @Date: 2022/11/24/10:05
 * @Description: 自动议价--议价标的过滤--按议价规则(前几名供应商)
 * 竞争性磋商自动议价方案（总金额）
 */
public class ConsultationFilterByRule implements IExtFilterPlugin<ExtFilterContext> {
    private static final long serialVersionUID = 1L;

    public ConsultationFilterByRule() {
    }

    @Override
    public Map<String, Object> getQFilter(ExtFilterContext context) {
        return this.getQFilterMap(this.getQFilterByNegRule(context), (StringBuilder) null);
    }

    private QFilter getQFilterByNegRule(ExtFilterContext context) {
        long projectId = PdsCommonUtils.object2Long(context.getParamMap().get("projectid"));
        QFilter compareFilter = new QFilter("id", "=", projectId);
        DynamicObject compare = QueryServiceHelper.queryOne("src_compare", "id,billno", compareFilter.toArray());
        if (null == compare) {
            return null;
        } else {
            String billNo = compare.getString("billno");
            QFilter filter = new QFilter("project.billno", "=", billNo);
            int size = QueryServiceHelper.query("src_negotiatebill", "id", filter.toArray()).size();
            if (size == 0) {
                QFilter bizFilter = new QFilter("billid", "=", projectId);
                DynamicObjectCollection bidassess = QueryServiceHelper.query("src_bidassess_biz", "id,rank,sumscore,supplier", bizFilter.toArray());
                //首轮
                DynamicObject compareConfig = QueryServiceHelper.queryOne("src_compare_config", "negotiaterule,topsupplier", compareFilter.toArray());
                if (null == compareConfig) {
                    return null;
                } else {
                    List<Long> resultSuppliers = new ArrayList<>();
                    int topSupplier = compareConfig.getInt("topsupplier");
                    for (DynamicObject object : bidassess) {
                        int rank = object.getInt("rank");
                        if (rank <= topSupplier) {
                            resultSuppliers.add(object.getLong("supplier"));
                        }
                    }
                    filter.and("turns", "=", "1").and("supplier.id", "in", resultSuppliers);
                    DynamicObjectCollection analyseTools = QueryServiceHelper.query("src_analysetool", "id", filter.toArray());
                    if (analyseTools.isEmpty()) {
                        return null;
                    }
                    Set<Long> ids = analyseTools.stream().map(a -> a.getLong("id")).collect(Collectors.toSet());
                    return new QFilter("id", "in", ids);
                }
            } else {
                Set<Long> ids = FilterByRule.getIdsByScore(filter, compareFilter, projectId);
                return new QFilter("id", "in", ids);
            }
        }
    }
}
